﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogic.Connectivity.Entities;
using BankApplication.BusinessLogic;
using System.ServiceModel;
using System.Threading;


namespace BankApplication.UI
{
	public partial class LoginForm : Form
	{
		public LoginForm()
		{
			InitializeComponent();
		}

		private void okButton_Click(object sender, EventArgs e)
		{
			Credentials credentials = new Credentials();
			credentials.Login = loginTextBox.Text;
			credentials.Password = passwordTextBox.Text;

			try
			{
				SessionManager.Instance.EstablishSession(credentials);

				DialogResult = DialogResult.OK;
				Close();
			}
			catch (CommunicationException)
			{
				MessageBox.Show("Connection problems! Please, turn to system administrator", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				//Application.Exit();
			}
			catch (AuthorizationFailedException exception)
			{
				MessageBox.Show("Authorization failed!" + exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			catch (ServerException ex)
			{
				MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			catch (Exception ex)
			{
				MessageBox.Show("Application crashed unexpectedly!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				Application.Exit();
			}
		}

		private void cancelButton_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
		}

	}
}
